//
//  EnergyHeaderView.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/27.
//

#import "EnergyHeaderView.h"
#import "EnergyinfoView.h"
#import "EnergyNumView.h"
#import "EnergyInfoModel.h"
#import "EnergyPriceView.h"

@interface EnergyHeaderView()

@property (nonatomic,strong) UIImageView * backView;
@property (nonatomic,strong) UIImageView * infoView;
@property (nonatomic,strong) EnergyinfoView * peopleView;
@property (nonatomic,strong) EnergyNumView * numView;
@property (nonatomic,strong) UIImageView * descView;
@property (nonatomic,strong) UIButton * nextBtn;

@property (nonatomic,strong) EnergyPriceView * priceView;
@property (nonatomic,strong) UIButton * enterBtn;
@property (nonatomic,strong) HomeTipsView * tipsView;
@property (nonatomic,strong) EnergyInfoModel * model;
@end

@implementation EnergyHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        [self requestData];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:@"RefshCoin" object:nil];

    }
    return self;
}

-(void)setSubViewLayout{
    
    [self addSubview:self.backView];
    [self addSubview:self.priceView];
    [self addSubview:self.infoView];
    [self addSubview:self.peopleView];
    [self addSubview:self.numView];
    [self addSubview:self.descView];
    [self addSubview:self.nextBtn];
    [self addSubview:self.enterBtn];
    
}

-(void)getNotification:(id)sender{
    [self requestData];
}

-(void)nextBtnAction{
    
    id vc = [[NSClassFromString(@"EnergyListVC") alloc]init];
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
}

-(void)requestData{
    @WeakObj(self)
    [SPNetworkingIns GET:PORT(@"monetary-center/api/herbCoin/queryHerbCoinInfo") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        self.model = [EnergyInfoModel yy_modelWithDictionary:responseObject[@"data"]];
        [self.peopleView setInfoModel:self.model];
        [self.numView setInfoModel:self.model];
        [self.priceView setInfoModel:self.model];
        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
}

-(void)enterBtnAction{
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"monetary-center/api/herbCoin/collectHerbCoin") params:@{@"id":self.model.notCollectedEnergy} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        FhudShowStr(@"收取成功");
        [self requestData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(void)calculateElvesRecoveryCoin:(NSString * )ID{
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"monetary-center/api/herbCoin/calculateElvesRecoveryCoinNew") params:@{@"pickerId":ID} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        NSString * energy = K_TimeJson(responseObject, @"data");
        [self.tipsView startAnimationWithTitle:@"恢复体力" info:[NSString stringWithFormat:@"您确定要花费%@草药币来恢复体力吗",energy]];
        [self.tipsView setEnterdBtnBlock:^{
            [SPNetworkingIns POST:PORT(@"monetary-center/api/herbCoin/elvesRecovery") params:@{@"pickerId":ID} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
                @StrongObj(self)
                FhudShowStr(@"恢复成功");
                [self requestData];
            } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            }];
        }];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}



-(UIImageView *)backView{
    
    if(!_backView){
        _backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 400 * FMScaleX)];
        _backView.image = [UIImage imageNamed:@"Energy_bg_icon"];
    }
    return _backView;
}

-(EnergyPriceView *)priceView{
    
    if(!_priceView){
        _priceView = [[EnergyPriceView alloc]initWithFrame:CGRectMake(0, StatusBar + 10 * FMScaleX, 183 * FMScaleX, 80 * FMScaleX)];
    }
    return _priceView;
}

-(UIButton *)enterBtn{
    
    if(!_enterBtn){
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH - 100 * FMScaleX, StatusBar + 10 * FMScaleX, 80 * FMScaleX, 80 * FMScaleX)];
        [_enterBtn setImage:[UIImage imageNamed:@"Energy_enter_icon"] forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterBtn;
}

-(UIImageView *)infoView{
    
    if(!_infoView){
        _infoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, GETMaxY(self.backView) - 57 * FMScaleX, WIDTH, 57 * FMScaleX)];
        _infoView.image = [UIImage imageNamed:@"Energy_info_icon"];
    }
    return _infoView;
}

-(EnergyinfoView *)peopleView{
    
    if(!_peopleView){
        _peopleView = [[EnergyinfoView alloc]initWithFrame:CGRectMake(0, GETMinY(self.infoView) - 255 * FMScaleX, WIDTH, 230 * FMScaleX)];
    }
    return _peopleView;
}
-(EnergyNumView *)numView{
    
    if(!_numView){
        _numView = [[EnergyNumView alloc]initWithFrame:CGRectMake((WIDTH - 294 * FMScaleX) / 2, GETMinY(self.infoView) - 47 * FMScaleX, 294 * FMScaleX, 50 * FMScaleX)];
        @WeakObj(self)
        _numView.enterdBtnBlock = ^(NSString * _Nonnull ID) {
            @StrongObj(self)
            if (ID.length > 0) {
                [self calculateElvesRecoveryCoin:ID];
            }else{
                FhudShowStr(@"您还没有草药精灵，无法恢复体力")
                return;
            }
        };
    }
    return _numView;
}
-(UIImageView *)descView{
    
    if (!_descView) {
        _descView = [[UIImageView alloc]initWithFrame:CGRectMake(26 * FMScaleX, GETMaxY(self.infoView) + 14 * FMScaleX, 160 * FMScaleX, 25 * FMScaleX)];
        _descView.image = [UIImage imageNamed:@"Energy_info_desc"];
    }
    return _descView;
}
-(UIButton *)nextBtn{
    
    if (!_nextBtn) {
        _nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH - 40 * FMScaleX, GETMaxY(self.infoView) + 8 * FMScaleX, 30 * FMScaleX, 24 * FMScaleX)];
        [_nextBtn setImage:[UIImage imageNamed:@"Me_head_arrow"] forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

-(HomeTipsView *)tipsView{
    
    if (!_tipsView) {
        _tipsView = [[HomeTipsView alloc]initWithFrame:[UIViewController currentViewController].view.bounds];
        [[UIViewController currentViewController].view addSubview:_tipsView];
    }
    return _tipsView;
}
@end
